package com.qbw.core;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.databinding.BindingAdapter;
import android.graphics.drawable.Drawable;
import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
import android.text.Html;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.image.fresco.FrescoUtil;
import com.lib.util.ResUtil;
import com.qbw.app.R;

/**
 * @author qinbaowei
 * @createtime 2017/04/10 10:54
 * @email qbaowei@qq.com
 * @description
 */


public class DataBindingAdapter {

    @BindingAdapter("viewWidth")
    public static void setViewWidth(View view, int width) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = width;
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("viewHeight")
    public static void setViewHeight(View view, int height) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = height;
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("viewSize")
    public static void setViewSize(View view, int size) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = size;
        layoutParams.height = size;
        view.setLayoutParams(layoutParams);
    }

    @BindingAdapter("viewSize")
    public static void setViewSize(View view, float size) {
        setViewSize(view, (int) size);
    }

    @BindingAdapter("htmlText")
    public static void setHtmlText(TextView textView, String text) {
        textView.setText(Html.fromHtml(text));
    }

    @BindingAdapter("pbMax")
    public static void setPbMax(ProgressBar progressBar, int max) {
        progressBar.setMax(max);
    }

    @BindingAdapter("pbProgress")
    public static void setPbProgress(ProgressBar progressBar, int progress) {
        if (progress < 0) {
            progress = 0;
        }
        progressBar.setProgress(progress);
    }

    @BindingAdapter("vEnabled")
    public static void setVEnabled(View view, boolean enabled) {
        view.setEnabled(enabled);
    }

    /*@BindingAdapter("dbProgress")
    public static void setDbProgress(DonutProgress dbProgress, int progress) {
        dbProgress.setProgress(progress);
    }*/

    @BindingAdapter("src30")
    public static void setSrc30(SimpleDraweeView simpleDraweeView, String url) {
        int size = ResUtil.getDimension(R.dimen.avatar_size_30);
        FrescoUtil.loadImage(simpleDraweeView, url, size, size);
    }

    @BindingAdapter("src60")
    public static void setSrc60(SimpleDraweeView simpleDraweeView, String url) {
        int size = ResUtil.getDimension(R.dimen.avatar_size_60);
        FrescoUtil.loadImage(simpleDraweeView, url, size, size);
    }

    @BindingAdapter("src80")
    public static void setSrc80(SimpleDraweeView simpleDraweeView, String url) {
        int size = ResUtil.getDimension(R.dimen.avatar_size_80);
        FrescoUtil.loadImage(simpleDraweeView, url, size, size);
    }

    @BindingAdapter("imgSrcRes")
    public static void setImgSrcRes(ImageView imageView, @DrawableRes int res) {
        imageView.setImageResource(res);
    }

    @BindingAdapter("imgSrc")
    public static void setImgSrc(ImageView imageView, Drawable res) {
        imageView.setImageDrawable(res);
    }

    @BindingAdapter("tvTextRes")
    public static void setTVTextRes(TextView textView, @StringRes int res) {
        textView.setText(res);
    }

    @BindingAdapter("etShowPassword")
    public static void setEtShowPassword(EditText editText, boolean showPassword) {
        if (showPassword) {
            editText.setTransformationMethod(null);
        } else {
            editText.setTransformationMethod(new PasswordTransformationMethod());
        }
        editText.setSelection(editText.length());
    }

    @BindingAdapter("vSelected")
    public static void setSelected(View view, boolean selected) {
        view.setSelected(selected);
    }

    @BindingAdapter("cbChecked")
    public static void setCbChecked(CheckBox checkBox, boolean checked) {
        checkBox.setChecked(checked);
    }

    @BindingAdapter("txtDrawableLeft")
    public static void setTxtDrawableLeft(TextView textView, Drawable drawable) {
        textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
    }

    /*@BindingAdapter("arcFinishedColor")
    public static void setArcFinishedColor(ArcProgress progress, int color) {
        progress.setFinishedStrokeColor(color);
    }*/

    @BindingAdapter("viewConstraintDimensionRatio")
    public static void setViewConstraintDimensionRatio(View view, String dimensionRation) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (params instanceof ConstraintLayout.LayoutParams) {
            ConstraintLayout.LayoutParams clp = (ConstraintLayout.LayoutParams) params;
            clp.dimensionRatio = dimensionRation;
            view.setLayoutParams(clp);
        }
    }

    @BindingAdapter("viewShowGone")
    public static void setViewShowGone(View view, boolean show) {
        view.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @BindingAdapter("viewShowInvisible")
    public static void setViewShowInvisible(View view, boolean show) {
        view.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
    }

    @BindingAdapter("m2Km")
    public static void setM2Km(TextView tv, int m) {
        float km = m / 1000F;
        tv.setText(String.format("%.1fkm", km));
    }

    @BindingAdapter("meter")
    public static void setMeter(TextView tv, int m) {
        tv.setText(String.format("%dm", m));
    }
}
